using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Caching;

namespace _18miljard.Mail
{
	public class MailCache
	{
		internal static EMail GetMail(string mailName)
		{
			string cacheName = "mail_" + mailName;

			EMail cachedMail = MailCache.Cache[cacheName] as EMail;

			if (cachedMail == null)
			{
				cachedMail = new EMail(mailName);

				CacheDependency dependency = new CacheDependency(HttpContext.Current.Server.MapPath("/App_Data/Mail.xml"));

				MailCache.Cache.Insert(cacheName, cachedMail, dependency);
			}

			EMail mail = new EMail(cachedMail);
			mail.SetSendBody(mailName != "tellafriend");

			return mail;
		}

		internal static PostScriptumCollection PostScriptums
		{
			get
			{
				string cacheName = "postscriptums";

				PostScriptumCollection postScriptums = MailCache.Cache[cacheName] as PostScriptumCollection;

				if (postScriptums == null)
				{
					postScriptums = new PostScriptumCollection();

					CacheDependency dependency = new CacheDependency(HttpContext.Current.Server.MapPath("/App_Data/Mail.xml"));

					MailCache.Cache.Insert(cacheName, postScriptums, dependency);
				}

				return postScriptums;
			}
		}

		private static Cache Cache
		{
			get { return HttpContext.Current.Cache; }
		}
	}
}
